package com.zimi.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zimi.mapper.SysOrgCatePrivilegeMapper;
import com.zimi.mapper.SysOrgExtendMapper;
import com.zimi.model.SysOrgCatePrivilege;
import com.zimi.model.SysOrgExtend;
import com.zimi.model.SysUser;
import com.zimi.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 机构分类功能授权表 Service
 *
 * @author lnintr
 * @since 2019-05-27
 */
@Service
public class SysOrgCatePrivilegeService {
    @Autowired
    private SysOrgExtendMapper sysOrgExtendMapper;

    @Autowired
    private SysOrgCatePrivilegeMapper sysOrgCatePrivilegeMapper;

    /*
     * 取得权限和机构信息
     * */
    public Object getFunctionlistByOrgCate(SysOrgExtend sysOrgExtend) {
        // 1)取得所有功能
        List<SysOrgExtend> list = sysOrgExtendMapper.selectFunctionList(sysOrgExtend);

        if (list != null && list.size() > 0) {
            // 2)取得所有功能信息
            List<SysOrgCatePrivilege> listFunction = sysOrgCatePrivilegeMapper.selectList(new QueryWrapper<SysOrgCatePrivilege>().lambda().eq(SysOrgCatePrivilege::getOrgCateId, sysOrgExtend.getOrgCateId()));
            Map<Integer, SysOrgCatePrivilege> orgCateMap = null;

            if (listFunction != null && listFunction.size() > 0) {
                orgCateMap = new HashMap<>();
                for (SysOrgCatePrivilege sop : listFunction) {
                    orgCateMap.put(sop.getFunctionId(), sop);
                }
            }

            // 设置选中标志的值
            for (SysOrgExtend soe : list) {
                if (orgCateMap != null) {
                    SysOrgCatePrivilege sysOrgPrivilege = (SysOrgCatePrivilege) orgCateMap.get(soe.getFunctionId());

                    if (sysOrgPrivilege != null) {
                        soe.setId(sysOrgPrivilege.getId());
                        soe.setUpdateTime(sysOrgPrivilege.getUpdateTime());

                        if (sysOrgPrivilege.getPrivilegeState() == 1) {
                            soe.setChoiceFlag(true);
                        } else {
                            soe.setChoiceFlag(false);
                        }
                    } else {
                        soe.setChoiceFlag(false);
                        soe.setId(0);
                    }
                } else {
                    soe.setChoiceFlag(false);
                    soe.setId(0);
                }
            }
        }

        return list;
    }

    @Transactional
    public void saveFunctionOrg(List<SysOrgExtend> sysOrgExtends, int flag) {
        List<Integer> idList = new ArrayList<>();
        if (sysOrgExtends != null && sysOrgExtends.size() > 0) {

            int orgCateId = sysOrgExtends.get(0).getOrgCateId();
            SysOrgCatePrivilege tmpOrgExtend = null;

            for (SysOrgExtend soe : sysOrgExtends) {
                idList.add(soe.getFunctionId());
            }

            // 1）对要修改的对象加行锁
            List<SysOrgCatePrivilege> results = sysOrgCatePrivilegeMapper.selectList(new QueryWrapper<SysOrgCatePrivilege>().lambda().eq(SysOrgCatePrivilege::getOrgCateId, orgCateId).in(SysOrgCatePrivilege::getFunctionId, idList).last("for update"));

            Map<Integer, SysOrgCatePrivilege> resMap = null;

            if (results != null && results.size() > 0) {
                resMap = new HashMap<>();

                for (SysOrgCatePrivilege socp : results) {
                    resMap.put(socp.getFunctionId(), socp);
                }
            }

            SysUser user = UserUtil.getUser();
            Date updateTime = new Date();

            // 2)所属权限创建或修改
            for (SysOrgExtend soe : sysOrgExtends) {
                if (resMap != null) {
                    tmpOrgExtend = resMap.get(soe.getFunctionId());

                    // 3）判断该数据已经被改过没有
                    if (tmpOrgExtend != null) {
                        if (tmpOrgExtend.getUpdateTime() != null) {
                            if (soe.getUpdateTime() != null && soe.getUpdateTime().getTime() != tmpOrgExtend.getUpdateTime().getTime()) {
                                throw new RuntimeException("修改数据失败，数据已经被其他用户改过。");
                            }
                        }

                        tmpOrgExtend.setUpdateUser(user.getId());
                        tmpOrgExtend.setUpdateTime(updateTime);
                        tmpOrgExtend.setPrivilegeState(flag);
                        tmpOrgExtend.updateById();
                    } else {
                        tmpOrgExtend = new SysOrgCatePrivilege();

                        tmpOrgExtend.setCreateUser(user.getId());
                        tmpOrgExtend.setCreateTime(updateTime);
                        tmpOrgExtend.setPrivilegeState(flag);
                        tmpOrgExtend.setOrgCateId(orgCateId);
                        tmpOrgExtend.setFunctionId(soe.getFunctionId());

                        tmpOrgExtend.insert();
                    }
                } else {
                    tmpOrgExtend = new SysOrgCatePrivilege();

                    tmpOrgExtend.setCreateUser(user.getId());
                    tmpOrgExtend.setCreateTime(updateTime);
                    tmpOrgExtend.setPrivilegeState(flag);
                    tmpOrgExtend.setOrgCateId(orgCateId);
                    tmpOrgExtend.setFunctionId(soe.getFunctionId());

                    tmpOrgExtend.insert();
                }
            }
        }
    }
}
